$(function(){
    pageInit();
});

function pageInit(){

    var url2={};

    var url1 = location.search;
    var workMessageId='';
    if (url1.indexOf("?") != -1) {
        workMessageId = url1.substr(4);
    }
    var url0 = server + '/server/business/leaderRest/verify/'+workMessageId+'.json';

    var grid_selector = $("#salary-table");
    var pager_selector = $("#pager2");


    //创建jqGrid组件
    jQuery(grid_selector).jqGrid({
            jsonReader : {
                   root:"data",
                   page: "page",
                   total: "total",
                   records: "records",
                   cell: "cell"
            },
            url : url0,//组件创建完成之后请求数据的url
            datatype : "json",//请求数据返回的类型。可选json,xml,txt
            colNames : [ 'id','messageId','姓名', '手机号','打分','工资', '备注'],//jqGrid的列显示名字
            colModel : [ //jqGrid每一列的配置信息。包括名字，索引，宽度,对齐方式.....
                {name : 'userId',index : 'userId',width: 20,align:'center'},
                {name : 'messageId',index : 'messageId',width: 20,hidden:true},
                {name : 'name',index : 'name ',width: 20,editable:false,},
                {name : 'phone',index : 'phone',width: 20,editable:false},
                {name : 'grade',index : 'grade',width: 20,editable:true},
                {name : 'salary',index :'salary',width: 20,editable:true},
                {name : 'note',index:'note', width: 30,editable:true}
            ],
            rowNum : 10,
            height: 'auto',
            editurl : './someurl.php',
            pager : pager_selector,
            altRows: true,
            autowidth:true,
            cellEdit:true,
            viewrecords : true,
            shrinkToFit:true,
            cellsubmit: "clientArray",
            loadComplete : function() {
                var table = this;
                setTimeout(function(){
                    testJqfrid();
                    updatePagerIcons(table);
                    enableTooltips(table);
                }, 0);
            }
        });

    jQuery(grid_selector).jqGrid('navGrid',pager_selector, {edit : false,add : false,del : false});

    $(window).resize(function(){
        $(".salary-table").setGridWidth($(window).width());
    });




    //replace icons with FontAwesome icons like above
    function updatePagerIcons(table) {
        var replacement =
        {
            'ui-icon-seek-first' : 'icon-double-angle-left bigger-140',
            'ui-icon-seek-prev' : 'icon-angle-left bigger-140',
            'ui-icon-seek-next' : 'icon-angle-right bigger-140',
            'ui-icon-seek-end' : 'icon-double-angle-right bigger-140'
        };
        $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function(){
            var icon = $(this);
            var $class = $.trim(icon.attr('class').replace('ui-icon', ''));

            if($class in replacement) icon.attr('class', 'ui-icon '+replacement[$class]);
        })
    }

    function enableTooltips(table) {
        $('.navtable .ui-pg-button').tooltip({container:'body'});
        $(table).find('.ui-pg-div').tooltip({container:'body'});
    }

}
function testJqfrid(){
    $("body").on('change', ':input[type=text]',function() {

        var aa = $(this).val();
        var obj = $("#salary-table").jqGrid("getRowData");//获取整个表格内容

        var row = $(this).parent().parent().attr('id') - 1;
        var line = $(this).attr('name');
        obj[row][line] = aa;

        sessionStorage.json = JSON.stringify(obj);//保存至浏览器仓库（存活一次session时间）

    });

}